package com.twang.stock.common;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.common.http.HttpClientService;
import com.twang.stock.dto.TenXunStock;
import com.twang.stock.entity.KaiPanLaBanKuai;
import com.twang.stock.entity.KaiPanLaZhangTingInfo;
import com.twang.stock.utils.date.HolidayCopy;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.assertj.core.util.Lists;
import org.springframework.util.CollectionUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

public class KaiPanLaCommon {



    /**
     *
     * @param data
     * @param 板
     */
    public static void 获取个数( List<Integer> data, List<KaiPanLaZhangTingInfo> 板){
        if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(板)){
            data.add(板.size());
        }else{
            data.add(0);
        }
    }






        /**
         * 查询涨停原因板块
         *
         * @date 2024-01-09
         * @return
         * @throws Exception
         */
    public static List<KaiPanLaZhangTingInfo> 查询涨停原因板块(int lbs) throws Exception {
        List<KaiPanLaZhangTingInfo> kaiPanLaZhangTingInfoList= Lists.newArrayList();
        List<NameValuePair> nameValuePairList = Lists.newArrayList();
        nameValuePairList.add(new BasicNameValuePair("Index", "0"));
        nameValuePairList.add(new BasicNameValuePair("Order", "0"));
        nameValuePairList.add(new BasicNameValuePair("PhoneOSNew", "2"));
        nameValuePairList.add(new BasicNameValuePair("PidType", lbs+""));
        nameValuePairList.add(new BasicNameValuePair("Type", "4"));
        nameValuePairList.add(new BasicNameValuePair("VerSion", "5.13.0.3"));
        nameValuePairList.add(new BasicNameValuePair("apiv", "w35"));
        nameValuePairList.add(new BasicNameValuePair("a", "DailyLimitPerformance"));
        nameValuePairList.add(new BasicNameValuePair("c", "HomeDingPan"));
        nameValuePairList.add(new BasicNameValuePair("st", "1000"));
        Object str = HttpClientService.sendPost("https://apphq.longhuvip.com/w1/api/index.php", nameValuePairList);
        String minutes= HolidayCopy.getHHmmss().substring(8,12);
        String date=HolidayCopy.getRealToday();
        if (null != str) {
            JSONObject result = (JSONObject) JSONObject.toJSON(str);
            List objectList= ((List)((List) result.get("info")).get(0));
            if(!CollectionUtils.isEmpty(objectList)){
                objectList.stream().forEach(item->{
                    KaiPanLaZhangTingInfo kaiPanLaZhangTingInfo=new KaiPanLaZhangTingInfo();
                    List listData=((List)item);
                    String code=  listData.get(0).toString();//code
                    kaiPanLaZhangTingInfo.setCode(code);
                    String name=  listData.get(1).toString();//name
                    kaiPanLaZhangTingInfo.setName(name);
                    String 封单金额=  listData.get(4).toString();
                    kaiPanLaZhangTingInfo.setFdje(封单金额);
                    String 涨停原因=  listData.get(5).toString();
                    kaiPanLaZhangTingInfo.setZtyy(涨停原因);
                    // String 当前封单金额=  listData.get(6).toString();
                    String 最大封单金额=  listData.get(7).toString();
                    kaiPanLaZhangTingInfo.setZdfdje(new BigDecimal(最大封单金额));
                    String 主力净额=  listData.get(8).toString();
                    kaiPanLaZhangTingInfo.setZlje(new BigDecimal(主力净额));
                    String 主力买入=  listData.get(9).toString();
                    kaiPanLaZhangTingInfo.setZlmr(new BigDecimal(主力买入));
                    String 主力卖出=  listData.get(10).toString();
                    kaiPanLaZhangTingInfo.setZlmc(new BigDecimal(主力卖出));
                    String 成交额=  listData.get(11).toString();
                    kaiPanLaZhangTingInfo.setCje(new BigDecimal(成交额));
                    String 主要关联板宽=  listData.get(12).toString();
                    kaiPanLaZhangTingInfo.setZyglbk(主要关联板宽);
                    String 实际流通市值=  listData.get(13).toString();
                    kaiPanLaZhangTingInfo.setSjltsz(new BigDecimal(实际流通市值));
                    String 实际换手率=  listData.get(14).toString();
                    kaiPanLaZhangTingInfo.setSjhsl(new BigDecimal(实际换手率));
                    String 连板数=  listData.get(15).toString();
                    kaiPanLaZhangTingInfo.setLbs(Integer.valueOf(连板数));
                    String 震幅=  listData.get(17).toString();
                    kaiPanLaZhangTingInfo.setZf(new BigDecimal(震幅));
                    String 板块=  listData.get(19).toString();
                    kaiPanLaZhangTingInfo.setBk(板块);
                    kaiPanLaZhangTingInfo.setCreateTime(new Date());
                    kaiPanLaZhangTingInfo.setUpdateTime(new Date());
                    kaiPanLaZhangTingInfo.setDate(date);
                    kaiPanLaZhangTingInfo.setMinutes(minutes);
                    kaiPanLaZhangTingInfoList.add(kaiPanLaZhangTingInfo);
                });
            }
            return kaiPanLaZhangTingInfoList;
        }
        return null;
    }

    /**
     * 查询板块
     * @return
     * @throws Exception
     */
    public static List<KaiPanLaBanKuai> getBanKuaiMinutes(String dates,String minutes,String rStart,String rEnd,String order) throws Exception {
        String url="https://apphwhq.longhuvip.com/w1/api/index.php";
        List<KaiPanLaBanKuai> kaiPanLaBanKuais = com.google.common.collect.Lists.newArrayList();
        List<NameValuePair> nameValuePairList = com.google.common.collect.Lists.newArrayList();
        if (StringUtils.isNotBlank(dates)) {
            nameValuePairList.add(new BasicNameValuePair("Date", dates));
            nameValuePairList.add(new BasicNameValuePair("Type", "-4"));
            url="https://apphis.longhuvip.com/w1/api/index.php";
            nameValuePairList.add(new BasicNameValuePair("st", "70"));
        }else{
            nameValuePairList.add(new BasicNameValuePair("Type", "2"));
            nameValuePairList.add(new BasicNameValuePair("st", "80"));
        }
        nameValuePairList.add(new BasicNameValuePair("Index", "0"));
        nameValuePairList.add(new BasicNameValuePair("Order", order));
        nameValuePairList.add(new BasicNameValuePair("PhoneOSNew", "2"));
        nameValuePairList.add(new BasicNameValuePair("ZSType", "4"));
        nameValuePairList.add(new BasicNameValuePair("VerSion", "5.13.0.3"));
        nameValuePairList.add(new BasicNameValuePair("apiv", "w35"));
        nameValuePairList.add(new BasicNameValuePair("a", "RealRankingInfo"));
        nameValuePairList.add(new BasicNameValuePair("c", "ZhiShuRanking"));
        nameValuePairList.add(new BasicNameValuePair("RStart", rStart));//开始时间
        nameValuePairList.add(new BasicNameValuePair("REnd", rEnd));//结束时间
        Object str = HttpClientService.sendPost(url, nameValuePairList);
        String date = HolidayCopy.getRealToday();
        if (null != str) {
            JSONObject result = (JSONObject) JSONObject.toJSON(str);
            List objectList = (List) result.get("list");
            if (!CollectionUtils.isEmpty(objectList)) {
                objectList.stream().forEach(item -> {
                    KaiPanLaBanKuai kaiPanLaBanKuai = new KaiPanLaBanKuai();
                    List listData = ((List) item);
                    String code = listData.get(0).toString();//code
                    kaiPanLaBanKuai.setCode(code);
                    String name = listData.get(1).toString();//name
                    kaiPanLaBanKuai.setName(name);
                    kaiPanLaBanKuai.setZhangDie(new BigDecimal(listData.get(3).toString()));
                    kaiPanLaBanKuai.setZhangShu(new BigDecimal(listData.get(4).toString()));
                    kaiPanLaBanKuai.setAmount(new BigDecimal(listData.get(5).toString()));
                    kaiPanLaBanKuai.setZhuLiJingAmount(new BigDecimal(listData.get(6).toString()));
                    kaiPanLaBanKuai.setLb(new BigDecimal(listData.get(9).toString()));
                    kaiPanLaBanKuai.setQuJianZhangFu(new BigDecimal(listData.get(11).toString()));
                    kaiPanLaBanKuai.setCreateTime(new Date());
                    kaiPanLaBanKuai.setUpdateTime(new Date());
                    kaiPanLaBanKuai.setRStart(rStart);
                    kaiPanLaBanKuai.setREnd(rEnd);
                    kaiPanLaBanKuai.setOrderType(order);
                    kaiPanLaBanKuai.setDate(date);
                    kaiPanLaBanKuai.setMinutes(minutes);
                    kaiPanLaBanKuais.add(kaiPanLaBanKuai);
                });
            }
        }
        return kaiPanLaBanKuais;
    }
}
